<template>
  <div class="aui-wrapper aui-page__login">
    <div class="aui-content__wrapper">
      <main class="aui-content">
        <div class="login-header">
          <h2 class="login-brand">{{ $t('brand.lg') }}</h2>
        </div>
        <div class="login-body">
          <h3 class="login-title">{{ $t('login.title') }}</h3>
          <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" status-icon>
            <el-form-item prop="account">
              <el-input v-model="dataForm.account" :placeholder="$t('login.account')">
                <span slot="prefix" class="el-input__icon">
                  <svg class="icon-svg" aria-hidden="true"><use xlink:href="#icon-user"></use></svg>
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="dataForm.password" type="password" :placeholder="$t('login.password')">
                <span slot="prefix" class="el-input__icon">
                  <svg class="icon-svg" aria-hidden="true"><use xlink:href="#icon-lock"></use></svg>
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="captchaCode" v-if="need">
              <el-row :gutter="20">
                <el-col :span="14">
                  <el-input  v-model="dataForm.captchaCode" :placeholder="$t('login.captcha')">
                    <span slot="prefix" class="el-input__icon">
                      <svg class="icon-svg" aria-hidden="true"><use xlink:href="#icon-safetycertificate"></use></svg>
                    </span>
                  </el-input>
                </el-col>
                <el-col :span="10" class="login-captcha">
                  <img :src="captchaImg" @click="getCaptcha()">
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="dataFormSubmitHandle()" class="w-percent-100">{{ $t('login.title') }}</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="login-footer">
          <p>
            <a href="https://gitee.com/sunalee/fastboot" target="_blank">{{ $t('login.demo') }}</a>
          </p>
          <p><a href="https://gitee.com/sunalee/fastboot-vue" target="_blank">{{ $t('login.copyright') }}</a>{{ thisYear }} © <a href="https://mp.weixin.qq.com/s?__biz=MzU5NDMyODA2MQ==&mid=100000095&idx=1&sn=ff09d8155c86d2f4ce5e3d12dee0224f&chksm=7e03a1cd497428dbd3baa1a8bbfe3148d7213c249c15175d2adb658facb0eedfc1aa8706879d#rd">iFillDream</a></p>
        </div>
      </main>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
import { getUUID } from '@/utils'
import { setMd5 } from '@/utils'
export default {
  data () {
    return {
      captchaImg: '',
	  need: false,
	  thisYear: new Date().getFullYear(),
      dataForm: {
        account: '',
        password: '',
        captchaKey: '',
        captchaCode: '123'
      }
    }
  },
  computed: {
    dataRule () {
      return {
        account: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        captchaCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getCaptcha()
  },
  methods: {
    // 获取验证码
    getCaptcha () {
		if( this.need ){
			this.$http.get('/common/captcha/get', this.dataForm).then(({ data: res }) => {
			  if (res.code == 200) {
					this.captchaImg = res.data.imageBase64;
					this.dataForm.captchaKey = res.data.key;
			  }
			})
			this.dataForm.captchaCode = ''
		}else{
			this.dataForm.captchaCode = '123'
			this.dataForm.captchaKey = "";
			this.dataForm.captchaCode = "";
		}
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
		var temp = JSON.parse( JSON.stringify(this.dataForm) );
		temp.password = setMd5(temp.password);
		this.$refs['dataForm'].validate((valid) => {
		if (!valid) {
		  return false
		}
		this.$http.post('/pc/systemUser/login', temp).then(({ data: res }) => {
			if (res.code === 200) {
				this.need = false;
				Cookies.set('token', res.data.token)
				Cookies.set('userImg', res.data.img)
				localStorage.setItem('current_permissions', JSON.stringify(res.data.access.codes));
				localStorage.setItem('current_user', JSON.stringify(res.data));
				this.$router.replace({ name: 'home' })
			}else{
				if( res.code === 9999 ){
					return this.$message.error(res.msg);
				}
				this.need = res.otherMap.need;
				this.$message.error(res.msg)
				this.getCaptcha();
			}
		}).catch(() => {
			this.$message.error('请求失败~')
		})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
